<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('room_members', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('room_id')->comment('房间ID');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->enum('role', ['creator', 'admin', 'member'])->default('member')->comment('角色');
            $table->timestamp('joined_at')->useCurrent()->comment('加入时间');
            $table->timestamp('last_active_at')->nullable()->comment('最后活跃时间');
            $table->boolean('is_active')->default(true)->comment('是否活跃');
            $table->json('settings')->nullable()->comment('个人设置');
            $table->boolean('is_pinned')->default(false)->comment('是否置顶');
            $table->integer('pin_order')->default(0)->comment('置顶排序');
            $table->timestamps();
            
            $table->foreign('room_id')->references('id')->on('rooms')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->unique(['room_id', 'user_id']);
            
            $table->index('room_id');
            $table->index('user_id');
            $table->index('role');
            $table->index('joined_at');
            $table->index('last_active_at');
            $table->index('is_pinned');
            $table->index('pin_order');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('room_members');
    }
};